var cfg = require('../../utils/cfg');
const { $Toast } = require('../../dist/base/index');
const app = getApp();
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    commos: [],
    shangjiaFlag : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadCommodity();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  changeStatus : function(event) {
    let that = this;
    let flag = event.detail.value;
    let commoId = event.currentTarget.dataset.commoId

    sendRequest.sendReq(cfg.djurl.changeCommodityStatus, {
      openId: 'xxx',
      status: flag ? 1 : 0,
      commodityId: commoId
    }, function (result) {
      let commos = that.data.commos;
      for (let i=0; i<commos.length; i++) {
        if (commos[i].id == commoId) {
          commos[i].status = flag ? 1 : 0;
          commos[i].statusValue = flag ? '已上架' : '已下架';
          that.setData({
            commos: commos
          });
        }
      }
    });
  },

  toAdd : function() {
    wx.navigateTo({
      url: '/pages/caidanAdd/caidanAdd'
    })
  },

  //加载菜品
  loadCommodity: function () {
    let that = this;
    sendRequest.sendReq(cfg.djurl.listCommodity, {
      openId: 'xxx'
    }, function(result) {
      that.setData({
        commos: result.data
      });
    });

  }
})